REQUIRES: x86

RUN: split-file %s %ts
RUN: llvm-mc -filetype=obj -triple=x86_64 %ts/s -o %t.o

## Check that despite having a lower sort rank, an orphan section '.init_array'
## is placed after '.data' and '.data2' and in the same memory region.

## Also check that a non-SHF_ALLOC orphan section '.nonalloc' is not placed in
## a memory region. Both defined memory regions are exhausted after all expected
## sections are added, thus, trying to put any unexpected section would lead to
## an error.

RUN: ld.lld -o %t -T %ts/t %t.o
RUN: llvm-readelf -S %t | FileCheck %s

CHECK: Name        Type       Address          Off           Size
CHECK: .text       PROGBITS   0000000000008000 {{[0-9a-f]+}} 000004
CHECK: .data       PROGBITS   0000000000009000 {{[0-9a-f]+}} 000008
CHECK: .data2      PROGBITS   0000000000009008 {{[0-9a-f]+}} 00000c
CHECK: .init_array INIT_ARRAY 0000000000009014 {{[0-9a-f]+}} 000010
CHECK: .nonalloc   PROGBITS   0000000000000000 {{[0-9a-f]+}} 000010

## Check that attributes of memory regions are ignored for orphan sections when
## the anchor section specifies the memory region explicitly, This seems to
## contradict https://sourceware.org/binutils/docs/ld/MEMORY.html, but better
## resembles the way GNU ld actually works.

RUN: ld.lld -o %t2 -T %ts/t2 %t.o
RUN: llvm-readelf -S %t2 | FileCheck %s

## Same as the previous case, but now properties of sections conflict with
## memory region attributes. Still, orphan sections are placed in the same
## regions as their anchors.

RUN: ld.lld -o %t3 -T %ts/t3 %t.o
RUN: llvm-readelf -S %t3 | FileCheck %s

## Check that when memory regions for anchor sections are not specified
## explicitly and are selected by attributes, orphan sections are also assigned
## to memory regions by matching properties.

RUN: ld.lld -o %t4 -T %ts/t4 %t.o
RUN: llvm-readelf -S %t4 | FileCheck %s --check-prefix=CHECK4

CHECK4: Name        Type       Address          Off           Size
CHECK4: .text       PROGBITS   0000000000008000 {{[0-9a-f]+}} 000004
CHECK4: .init_array INIT_ARRAY 0000000000009000 {{[0-9a-f]+}} 000010
CHECK4: .data       PROGBITS   0000000000009010 {{[0-9a-f]+}} 000008
CHECK4: .data2      PROGBITS   0000000000009018 {{[0-9a-f]+}} 00000c
CHECK4: .nonalloc   PROGBITS   0000000000000000 {{[0-9a-f]+}} 000010

#--- s
  .text
  .zero 4

  .data
  .zero 8

  .section .data2,"aw",@progbits
  .zero 0xc

  .section .init_array,"aw",@init_array
  .zero 0x10

  .section .nonalloc,""
  .zero 0x10

#--- t
MEMORY
{
  TEXT : ORIGIN = 0x8000, LENGTH = 0x4
  DATA : ORIGIN = 0x9000, LENGTH = 0x24
}

SECTIONS
{
  .text : { *(.text) } > TEXT
  .data : { *(.data) } > DATA
}

#--- t2
MEMORY
{
  TEXT (rwx) : ORIGIN = 0x8000, LENGTH = 0x4
  DATA (rwx) : ORIGIN = 0x9000, LENGTH = 0x24
}

SECTIONS
{
  .text : { *(.text) } > TEXT
  .data : { *(.data) } > DATA
}

#--- t3
MEMORY
{
  TEXT (!w) : ORIGIN = 0x8000, LENGTH = 0x4
  DATA (!w) : ORIGIN = 0x9000, LENGTH = 0x24
}

SECTIONS
{
  .text : { *(.text) } > TEXT
  .data : { *(.data) } > DATA
}

#--- t4
MEMORY
{
  TEXT (rx)  : ORIGIN = 0x8000, LENGTH = 0x4
  DATA (w!x) : ORIGIN = 0x9000, LENGTH = 0x24
}

SECTIONS
{
  .text : { *(.text) }
}
